$().ready(function() {
	loadProject();
	loadComplany();
	loadinvoiceType();
	validateRule();
	laydate({
		elem : '#invoiceTime'
	});
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
function save() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/fin/billing/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			invoiceTime : {
				required : true
			},
			projectId : {
				required : true
			},
			billingComplayId : {
				required : true
			},
			receiptName : {
				required : true
			},
			invoiceAmount : {
				required : true
			}
		},
		messages : {
			invoiceTime : {
				required : icon + "发票日期必填"
			},
			projectId : {
				required : icon + "请选择发票所属项目"
			},
			billingComplayId : {
				required : icon + "请选择开票方"
			},
			receiptName : {
				required : icon + "收票方必填"
			},
			invoiceAmount : {
				required : icon + "票额必填，且只能为数字"
			}
		}
	})
}
//项目
function loadProject(){
	var html = "";
	$.ajax({
		url : '/itm/project/getNotEndProjectlist',
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].id + '">' + data[i].name + '</option>'
			}
			$(".chosen-select-projectId").append(html);
			$(".chosen-select-projectId").chosen({
				maxHeight : 200,
				search_contains: true
			});
			//点击事件
			$('.chosen-select-projectId').on('change', function(e, params) {
				console.log(params.selected);
				var opt = {
					query : {
						type : params.selected,
					}
				}
				$('#exampleTable').bootstrapTable('refresh', opt);
				// $.ajax({
				// 	url :"/itm/project/get/"+params.selected,
				// 	data:null,
				// 	type:"get",
				// 	success : function(data) {
				// 		$("input[name='projectAmount']").val(Number(data.projectAmount));
				// 	}
				// });
			});
		}
	});
}

//开票方
function loadComplany(){
	var html = "";
	$.ajax({
		url : '/system/company/getCompanyList',
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].id + '">' + data[i].name + '</option>'
			}
			$(".chosen-select-billingComplayId").append(html);
			$(".chosen-select-billingComplayId").chosen({
				maxHeight : 200
			});
			//点击事件
			$('.chosen-select-billingComplayId').on('change', function(e, params) {
				console.log(params.selected);
				var opt = {
					query : {
						type : params.selected,
					}
				}
				$('#exampleTable').bootstrapTable('refresh', opt);
			});
		}
	});
}

//发票类型
function loadinvoiceType(){
	var html = "";
	$.ajax({
		url : '/fin/reimburseType/reimtypelist',
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].id + '">' + data[i].name + '</option>'
			}
			$(".chosen-select-invoiceTypeid").append(html);
			$(".chosen-select-invoiceTypeid").chosen({
				maxHeight : 200
			});
			//点击事件
			$('.chosen-select-invoiceTypeid').on('change', function(e, params) {
				console.log(params.selected);
				var opt = {
					query : {
						type : params.selected,
					}
				}
				$('#exampleTable').bootstrapTable('refresh', opt);
				$.ajax({
					url :"/fin/reimburseType/get/"+params.selected,
					data:null,
					type:"get",
					success : function(data) {
						$("input[name='taxRate']").val(Number(data.taxRate));
						total(data.taxRate || 0)
					}
				});
			});
		}
	});
}

function total(val){
	var invoiceAmount = $("#invoiceAmount").val()
	var taxVal = invoiceAmount * val
	var netVal =  invoiceAmount - taxVal
	$("#netAmount") .val(netVal);
	$("#taxAmount") .val(taxVal)
}


$("#invoiceAmount").change(function () {
	var val = $("#taxRate").val() || 0
	total(val)
})